﻿Imports System.Collections.Generic
Imports System.Reflection
Imports System.Threading
Imports System.Windows.Data
Imports System.Windows.Media
Imports System.Windows.Shapes

<TemplateVisualState(Name:="Active", GroupName:="Activity")> _
<TemplateVisualState(Name:="Inactive", GroupName:="Activity")> _
<TemplateVisualState(Name:="Displaying", GroupName:="ActivityDisplay")> _
<TemplateVisualState(Name:="Hidden", GroupName:="ActivityDisplay")> _
<StyleTypedProperty([Property]:="OverlayStyle", StyleTargetType:=GetType(Rectangle))> _
<StyleTypedProperty([Property]:="ProgressBarStyle", StyleTargetType:=GetType(ProgressBar))> _
Public Class Activity
    Inherits ContentControl

    Public Sub New()

        Me.DefaultStyleKey = GetType(Activity)
        VisualStateManager.GoToState(Me, "Inactive", True)
        VisualStateManager.GoToState(Me, "Hidden", True)
    End Sub

    ' Using a DependencyProperty as the backing store for IsActivityVisible. This enables animation, styling, binding, etc...
    Public Shared ReadOnly IsActivityVisibleProperty As DependencyProperty = DependencyProperty.Register("IsActivityVisible", GetType(Boolean), GetType(Activity), New PropertyMetadata(False))
    Public Property IsActivityVisible() As Boolean
        Get
            Return CBool(GetValue(IsActivityVisibleProperty))
        End Get
        Private Set(ByVal value As Boolean)
            SetValue(IsActivityVisibleProperty, value)
        End Set
    End Property


    ' Using a DependencyProperty as the backing store for IsActive. This enables animation, styling, binding, etc...
    Public Shared ReadOnly IsActiveProperty As DependencyProperty = DependencyProperty.Register("IsActive", GetType(Boolean), GetType(Activity), New PropertyMetadata(False, New PropertyChangedCallback(AddressOf sIsActiveChanged)))
    Public Property IsActive() As Boolean
        Get
            Return CBool(GetValue(IsActiveProperty))
        End Get
        Set(ByVal value As Boolean)
            SetValue(IsActiveProperty, value)
        End Set
    End Property

    Private Sub IsActiveChanged()
        If Me.IsActive Then
            VisualStateManager.GoToState(Me, "Active", True)
            Me.Dispatcher.BeginInvoke(AddressOf Activar)
        Else
            VisualStateManager.GoToState(Me, "Inactive", True)
            If IsActivityVisible Then
                Me.Dispatcher.BeginInvoke(AddressOf Desactivar)
            End If
        End If
    End Sub

    Private Sub Activar()
        If Me.IsActive Then
            IsActivityVisible = True
            VisualStateManager.GoToState(Me, "Displaying", True)
        End If
    End Sub

    Private Sub Desactivar()
        If Not Me.IsActive Then
            VisualStateManager.GoToState(Me, "Hidden", True)
            IsActivityVisible = False
        End If
    End Sub

    Private Shared Sub sIsActiveChanged(ByVal dep As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        TryCast(dep, Activity).IsActiveChanged()
    End Sub

    ' Using a DependencyProperty as the backing store for OverlayStyle. This enables animation, styling, binding, etc...
    Public Shared ReadOnly OverlayStyleProperty As DependencyProperty = DependencyProperty.Register("OverlayStyle", GetType(Style), GetType(Activity), New PropertyMetadata(Nothing))
    Public Property OverlayStyle() As Style
        Get
            Return DirectCast(GetValue(OverlayStyleProperty), Style)
        End Get
        Set(ByVal value As Style)
            SetValue(OverlayStyleProperty, value)
        End Set
    End Property


    ' Using a DependencyProperty as the backing store for ProgressBarStyle. This enables animation, styling, binding, etc...
    Public Shared ReadOnly ProgressBarStyleProperty As DependencyProperty = DependencyProperty.Register("ProgressBarStyle", GetType(Style), GetType(Activity), New PropertyMetadata(Nothing))
    Public Property ProgressBarStyle() As Style
        Get
            Return DirectCast(GetValue(ProgressBarStyleProperty), Style)
        End Get
        Set(ByVal value As Style)
            SetValue(ProgressBarStyleProperty, value)
        End Set
    End Property




End Class
